home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / zip.zip / SHIP.C < prev    next >
C/C++ Source or Header  |  1992-09-22  |  38KB  |  1,284 lines

  1. /* ship.c -- Not copyrighted 1991 Mark Adler */
  2.  
  3. #define SHIPVER "ship version 1.0  September 29, 1991  Mark Adler"
  4.  
  5. /* Command for mailing (-m): the %s's are, in order, the subject prefix,
  6.    the part number (always of the form "partnnnn"), the subject suffix
  7.    (empty or " (last)" if the last part), the mailing address, and the
  8.    name of the temporary file begin mailed.  The command "Mail" is for BSD
  9.    systems.  You may need to change it to "mailx" for System V Unix, using
  10.    the compile option "-DMAILX".  Also, on Sperry (Unisys?) SysV.3 systems,
  11.    you might try the command name "v6mail". */
  12.  
  13. #ifdef DIRENT           /* If compiled with zip, DIRENT implies System V */
  14. #  define MAILX
  15. #endif /* DIRENT */
  16.  
  17. #ifdef sun              /* Except Sun's use DIRENT, but have Mail */
  18. #  ifdef MAILX
  19. #    undef MAILX
  20. #  endif /* MAILX */
  21. #endif /* sun */
  22.  
  23. #ifdef sgi              /* Silicon Graphics that way too */
  24. #  ifdef MAILX
  25. #    undef MAILX
  26. #  endif /* MAILX */
  27. #endif /* sgi */
  28.  
  29. #ifdef VMS
  30. #  define TMPNAME "_SXXXXXX."
  31. #  define MAILCMD "mail %s /subj=\"%s %s%s\" \"%s\""
  32. #  define PATHCUT ']'
  33. #else /* !VMS */
  34. #  define TMPNAME "_SXXXXXX"
  35. #  ifdef MAILX
  36. #    define MAILCMD "mailx -s \"%s %s%s\" \"%s\" < %s"
  37. #  else /* !MAILX */
  38. #    define MAILCMD "Mail -s \"%s %s%s\" \"%s\" < %s"
  39. #  endif /* ?MAILX */
  40. #  define PATHCUT '/'
  41. #endif /* ?VMS */
  42.  
  43. /*
  44.  
  45. SHIP -
  46.  
  47.   Ship is a program for sending binary files through email.  It is designed
  48.   to supplant uuencode and uudecode.  Ship encodes approximately 6.23 bits
  49.   per character mailed, compared to uuencode's 5.73 bits per character.
  50.  
  51.   Ship also has these features: a 32-bit CRC check on each file; automatic
  52.   splitting of the ship output into multiple, smaller files for speedier
  53.   mailing; automatic mailing of ship's output, with subject lines for
  54.   multiple parts; and a check on the sequence of parts when unshipping.
  55.  
  56.   Usage:
  57.  
  58.        ship [-nnn] [-m address] [-s subject] file ...
  59.  
  60.   where nnn is the maximum number of K bytes for each output file, address
  61.   is the address to send mail to, subject is a Subject" line prefix, and
  62.   file ... is a list of files to ship.  If no options are given, ship
  63.   outputs to stdout.  The simplest use is:
  64.  
  65.        ship foo > x
  66.  
  67.   where foo is converted into the mailable file, x.
  68.  
  69.   When -nnn is specified, but -m is not, ship writes to the files
  70.   part0001, part0002, etc., where each file has nnn or less K bytes.  For
  71.   example:
  72.  
  73.        ship -25 bigfoo
  74.  
  75.   will write however many 25K byte or less ship files is needed to contain
  76.   bigfoo.  If, say, six files are needed, then the files part0001 to part0006
  77.   will be written.
  78.  
  79.   When using -m, nothing is written, either to files or to stdout; rather,
  80.   the output is mailed to the specified address.  If -nnn is also specified,
  81.   then the parts are mailed separately with the subject lines part0001, etc.
  82.   If -nnn is not specified, then only one part (the whole thing) is mailed
  83.   with the subject line "part0001".  For example:
  84.  
  85.        ship -25 -m fred bigfoo
  86.  
  87.   will mail the six parts of bigfoo to fred.
  88.  
  89.   Any number of files can be shipped at once.  They become part of one long
  90.   ship stream, so if, for example -25 is specified, all but the last part
  91.   will have about 25K bytes.  For example:
  92.  
  93.        ship -25 -m fred fee fi fo fum
  94.  
  95.   will send the files fee, fi, fo, and fum to fred.
  96.  
  97.   Fred will get several mail messages with the subject lines part0001, etc.
  98.   He can then save those messages as the files, say, p1, p2, p3, ...
  99.   Then he can use the command:
  100.  
  101.        ship -u p?
  102.  
  103.   to recreate bigfoo, or fee fi fo and fum, depending on what he was sent.
  104.   If Fred saved the wrong numbers, ship will detect this and report a
  105.   sequence error.
  106.  
  107.   Note: there is enough information in the shipped parts to determine the
  108.   correct sequence.  A future version of ship will prescan the files to
  109.   determine the sequence, and then process them in the correct order.
  110.  
  111.   If a file being received already exists, ship -u will report an error
  112.   and exit.  The -o option avoids this and allows ship to overwrite existing
  113.   files.  The -o option must follow the -u option:
  114.  
  115.        ship -u -o p?
  116.  
  117.   In addition to the -u option, ship will unship if it sees that its name is
  118.   unship.  On Unix systems, this can be done simply by linking the executable
  119.   to unship:
  120.  
  121.        ln ship unship 
  122.  
  123.   Ship can also be used as a filter.  The special file name "-" means stdin.
  124.   For example:
  125.  
  126.        tar covf - foodir | compress | ship -25 -m fred -
  127.  
  128.   will tar the directory foodir, compress it, and ship it to fred in 25K byte
  129.   pieces.  Then, after Fred saves the files as p01, etc. at the other, end,
  130.   he can:
  131.  
  132.        ship -u p? | zcat | tar xovf -
  133.  
  134.   which will recreate the directory foobar and its contents.  ship -u knows
  135.   to write to stdout, since the original ship put the special file name "-"
  136.   in the first part.
  137.  
  138.   Ship uses a base 85 coding that needs 32-bit multiplication and division.
  139.   This can be slow on 16-bit machines, so ship provides a fast encoding
  140.   method by specifying the -f option.  This method is somewhat faster even
  141.   on 32-bit machines, and has approximately a 1% penalty in the size of the
  142.   encoded result (-f gives 6.26 bits per character, on the average).  The -f
  143.   option need only be used when shipping--unshipping (ship -u) automatically
  144.   detects the encoding used.  For example:
  145.  
  146.        ship -f -25 -m fred foo
  147.  
  148.   will send foo to fred in 25K byte pieces using the fast encoding method.
  149.   You don't need to tell Fred, since ship -u will figure that out for him.
  150.  
  151.   The fast encoding method is probabilistic, so it's possible for the size
  152.   penalty to be worse than 1%, and it's also possible for the fast encoding
  153.   to produce a smaller result than base 85 encoding would, all depending on
  154.   the data.
  155.  
  156.   The -q option can be used with either ship or unship (ship -u) for quiet
  157.   operation--informational messages are inhibited.
  158.  
  159.   You can find out the version of ship and get the command usage by using
  160.   "ship -h" or "ship -?".  The version number and date and help will be
  161.   printed, and ship will exit (the rest of the command line is ignored).
  162.  
  163.   Acknowledgements:
  164.  
  165.   The hard-arithmetic coding algorithm was blatantly stolen from Peter
  166.   Gutmann's pgencode/pgdecode programs posted on comp.compression, with
  167.   modifications to use 86 instead of 94 characters, and to make zeros encode
  168.   better than, rather than worse than other bytes.  (As Stravinsky once said:
  169.   "Mediocre composers plagiarize.  Great composers steal.")
  170.  
  171. */
  172.  
  173. /* tailor.h -- Not copyrighted 1991 Mark Adler */
  174.  
  175. /* const's are inconsistently used across ANSI libraries--kill for all
  176.    header files. */
  177. #define const
  178.  
  179.  
  180. /* Use prototypes and ANSI libraries if __STDC__ */
  181. #ifdef __STDC__
  182. #  ifndef PROTO
  183. #    define PROTO
  184. #  endif /* !PROTO */
  185. #  define MODERN
  186. #endif /* __STDC__ */
  187.  
  188.  
  189. /* Use prototypes and ANSI libraries if Silicon Graphics */
  190. #ifdef sgi
  191. #  ifndef PROTO
  192. #    define PROTO
  193. #  endif /* !PROTO */
  194. #  define MODERN
  195. #endif /* sgi */
  196.  
  197.  
  198. /* Define MSDOS for Turbo C as well as Microsoft C */
  199. #ifdef __POWERC                 /* For Power C too */
  200. #  define __TURBOC__
  201. #endif /* __POWERC */
  202. #ifdef __TURBOC__
  203. #  ifndef MSDOS
  204. #    define MSDOS
  205. #  endif /* !MSDOS */
  206. #endif /* __TURBOC__ */
  207.  
  208.  
  209. /* Use prototypes and ANSI libraries if Microsoft or Borland C */
  210. #ifdef MSDOS
  211. #  ifndef PROTO
  212. #    define PROTO
  213. #  endif /* !PROTO */
  214. #  define MODERN
  215. #endif /* MSDOS */
  216.  
  217.  
  218. /* Turn off prototypes if requested */
  219. #ifdef NOPROTO
  220. #  ifdef PROTO
  221. #    undef PROTO
  222. #  endif /* PROTO */
  223. #endif /* NOPROT */
  224.  
  225.  
  226. /* Used to remove arguments in function prototypes for non-ANSI C */
  227. #ifdef PROTO
  228. #  define OF(a) a
  229. #else /* !PROTO */
  230. #  define OF(a) ()
  231. #endif /* ?PROTO */
  232.  
  233.  
  234. /* Allow far and huge allocation for small model (Microsoft C or Turbo C) */
  235. #ifdef MSDOS
  236. #  ifdef __TURBOC__
  237. #    include <alloc.h>
  238. #  else /* !__TURBOC__ */
  239. #    include <malloc.h>
  240. #    define farmalloc _fmalloc
  241. #    define farfree   _ffree
  242. #  endif /* ?__TURBOC__ */
  243. #else /* !MSDOS */
  244. #  define huge
  245. #  define far
  246. #  define near
  247. #  define farmalloc malloc
  248. #  define farfree   free
  249. #endif /* ?MSDOS */
  250.  
  251.  
  252. /* Define MSVMS if either MSDOS or VMS defined */
  253. #ifdef MSDOS
  254. #  define MSVMS
  255. #else /* !MSDOS */
  256. #  ifdef VMS
  257. #    define MSVMS
  258. #  endif /* VMS */
  259. #endif /* ?MSDOS */
  260.  
  261.  
  262. /* Define void, voidp, and extent (size_t) */
  263. #include <stdio.h>
  264. #ifdef MODERN
  265. #  ifndef M_XENIX
  266. #    include <stddef.h>
  267. #  endif /* !M_XENIX */
  268. #  include <stdlib.h>
  269.    typedef size_t extent;
  270.    typedef void voidp;
  271. #else /* !MODERN */
  272.    typedef unsigned int extent;
  273. #  define void int
  274.    typedef char voidp;
  275. #endif /* ?MODERN */
  276.  
  277. /* Get types and stat */
  278. #ifdef VMS
  279. #  include <types.h>
  280. #  include <stat.h>
  281. #else /* !VMS */
  282. #  include <sys/types.h>
  283. #  include <sys/stat.h>
  284. #endif /* ?VMS */
  285.  
  286.  
  287. /* Cheap fix for unlink on VMS */
  288. #ifdef VMS
  289. #  define unlink delete
  290. #endif /* VMS */
  291.  
  292.  
  293. /* For Pyramid */
  294. #ifdef pyr
  295. #  define strrchr rindex
  296. #  define ZMEM
  297. #endif /* pyr */
  298.  
  299.  
  300. /* File operations--use "b" for binary if allowed */
  301. #ifdef MODERN
  302. #  define FOPR "rb"
  303. #  define FOPM "r+b"
  304. #  define FOPW "w+b"
  305. #else /* !MODERN */
  306. #  define FOPR "r"
  307. #  define FOPM "r+"
  308. #  define FOPW "w+"
  309. #endif /* ?MODERN */
  310.  
  311.  
  312. /* Fine tuning */
  313. #ifndef MSDOS
  314. #   define BSZ 8192   /* Buffer size for files */
  315. #else /* !MSDOS */
  316. #   define BSZ 4096   /* Keep precious NEAR space */
  317.     /* BSZ can't be 8192 even for compact model because of 64K limitation
  318.      * in im_lmat.c. If you run out of memory when processing a large number
  319.      * files, use the compact model and reduce BSZ to 2048 here and in
  320.      * im_lm.asm.
  321.      */
  322. #endif /* ?MSDOS */
  323.  
  324. /* end of tailor.h */
  325.  
  326. #ifdef MODERN
  327. #  include <string.h>
  328. #else /* !MODERN */
  329.    voidp *malloc();
  330.    long atol();
  331.    char *strcpy();
  332.    char *strrchr();
  333. #endif /* ?MODERN */
  334.  
  335. /* Library functions not in (most) header files */
  336. char *mktemp OF((char *));
  337. int unlink OF((char *));
  338.  
  339. #ifdef MSDOS            /* Use binary mode for binary files */
  340. #  include <io.h>
  341. #  include <fcntl.h>
  342. #endif /* MSDOS */
  343.  
  344.  
  345. #define LNSZ 1025       /* size of line buffer */
  346.  
  347. typedef unsigned long ulg;      /* 32-bit unsigned integer */
  348.  
  349. typedef struct {        /* associates a CRC with a file */
  350.   FILE *f;              /* pointer to associated file stream */
  351.   ulg c;                /* CRC register */
  352.   ulg b;                /* four byte buffer */
  353.   int n;                /* buffer count */
  354. } cfile;
  355.  
  356.  
  357. /* Function prototypes */
  358. #ifdef MODERN
  359.    void err(int, char *);
  360.    cfile *chook(FILE *);
  361.    char *nopath(char *);
  362.    void newship(void);
  363.    void endship(int);
  364.    void newline(char *);
  365.    void ship(char *, FILE *);
  366.    void mkinv(void);
  367.    void decode(unsigned char *, cfile *);
  368.    void unship(char **, int, int);
  369.    void help(void);
  370.    void main(int, char **);
  371. #endif /* MODERN */
  372.  
  373.  
  374.  
  375. /* Globals for ship() */
  376. char sname[9];          /* current ship file name */
  377. FILE *sfile;            /* current ship file */
  378. ulg slns;               /* number of lines written to ship file */
  379. ulg slmax;              /* maximum number of lines per ship file */
  380. int fast;               /* true for arithmetic coding, else base 85 */
  381. int mail;               /* true if mailing */
  382. char mpspc[9];          /* prealloced space for prefix */
  383. char *mprefix = mpspc;  /* identification for this mailing */
  384. char *mdest;            /* mail destination */
  385. char mname[10];         /* temporary file name if mailing */
  386. ulg ccnt;               /* count of bytes read or written */
  387. int noisy = 1;          /* false to inhibit informational messages */
  388.  
  389.  
  390.  
  391. /* Errors */
  392. #define SE_ARG 1
  393. #define SE_FIND 2
  394. #define SE_NONE 3
  395. #define SE_PART 4
  396. #define SE_FORM 5
  397. #define SE_CONT 6
  398. #define SE_CRC 7
  399. #define SE_MAIL 8
  400. #define SE_OVER 9
  401. #define SE_FULL 10
  402. #define SE_MANY 11
  403. #define SE_MEM 12
  404. char *errors[] = {
  405.   /* 1 */ "invalid argument ",
  406.   /* 2 */ "could not find ",
  407.   /* 3 */ "no files received",
  408.   /* 4 */ "unfinished file ",
  409.   /* 5 */ "invalid ship format in ",
  410.   /* 6 */ "wrong sequence for ",
  411.   /* 7 */ "CRC check failed on ",
  412.   /* 8 */ "mail command failed: ",
  413.   /* 9 */ "attempted to overwrite ",
  414.   /* 10 */ "could not write to ",
  415.   /* 11 */ "too many output files!",
  416.   /* 12 */ "out of memory"
  417. };
  418.  
  419.  
  420. /* Set of 86 characters used for the base 85 digits (last one not used), and
  421.    the 86 character arithmetic coding.  Selected to be part of both the ASCII
  422.    printable characters, and the common EBCDIC printable characters whose
  423.    ASCII translations are universal. */
  424. unsigned char safe[] = {
  425.         '{','"','#','$','%','&','\'','(',')','*','+',',','-','.','/',
  426.         '0','1','2','3','4','5','6','7','8','9',':',';','<','=','>','?','@',
  427.         'A','B','C','D','E','F','G','H','I','J','K','L','M',
  428.         'N','O','P','Q','R','S','T','U','V','W','X','Y','Z','_',
  429.         'a','b','c','d','e','f','g','h','i','j','k','l','m',
  430.         'n','o','p','q','r','s','t','u','v','w','x','y','z','}'};
  431.  
  432. #define LOWSZ (sizeof(safe)-64)         /* low set size for fast coding */
  433.  
  434. /* Special replacement pairs--if first of each pair is received, it is
  435.    treated like the second member of the pair.  You're probably
  436.    wondering why.  The first pair is for compatibility with an
  437.    earlier version of ship that used ! for the base 85 zero digit.
  438.    However, there exist ASCII-EBCDIC translation tables that don't
  439.    know about exclamation marks.  The second set has mysterious
  440.    historical origins that are best left unspoken ... */
  441. unsigned char aliases[] = {'!','{','|','+',0};
  442.  
  443. /* Inverse of safe[], filled in by mkinv() */
  444. unsigned char invsafe[256];
  445.  
  446. /* Table of CRC-32's of all single byte values (made by makecrc.c) */
  447. ulg crctab[] = {
  448.   0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
  449.   0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
  450.   0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
  451.   0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
  452.   0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
  453.   0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
  454.   0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
  455.   0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
  456.   0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
  457.   0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
  458.   0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
  459.   0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
  460.   0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
  461.   0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
  462.   0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
  463.   0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
  464.   0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
  465.   0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
  466.   0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
  467.   0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
  468.   0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
  469.   0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
  470.   0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
  471.   0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
  472.   0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
  473.   0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
  474.   0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
  475.   0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
  476.   0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
  477.   0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
  478.   0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
  479.   0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
  480.   0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
  481.   0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
  482.   0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
  483.   0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
  484.   0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
  485.   0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
  486.   0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
  487.   0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
  488.   0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
  489.   0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
  490.   0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
  491.   0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
  492.   0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
  493.   0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
  494.   0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
  495.   0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
  496.   0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
  497.   0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
  498.   0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
  499.   0x2d02ef8dL
  500. };
  501.  
  502. /* Macro to update the CRC shift register one byte at a time */
  503. #define CRC(c,b) (crctab[((int)(c)^(int)(b))&0xff]^((c)>>8))
  504.  
  505.  
  506. char *errname = "ship error";
  507. char *warname = "ship warning";
  508.  
  509. void err(n, m)
  510. int n;                  /* error number */
  511. char *m;                /* additional error information */
  512. {
  513.   if (n == SE_FIND || n == SE_FULL)
  514.     perror(errname);
  515.   fputs(errname, stderr);
  516.   fputs(": ", stderr);
  517.   fputs(errors[n - 1], stderr);
  518.   fputs(m, stderr);
  519.   putc('\n', stderr);
  520.   if (*mname)
  521.     unlink(mname);
  522. #ifdef VMS
  523.   exit(0);
  524. #else /* !VMS */
  525.   exit(n);
  526. #endif /* ?VMS */
  527. }
  528.  
  529.  
  530. cfile *chook(f)
  531. FILE *f;                /* file stream */
  532. /* Inherit the file stream structure and add a CRC and buffer for appending
  533.    a CRC on reads and checking the CRC on writes.  Return a pointer to the
  534.    cfile structure, or NULL if the malloc() failed.  Also, if MSDOS, set the
  535.    file mode to binary to avoid LF<->CRLF conversions. */
  536. {
  537.   cfile *c;             /* allocated cfile structure */
  538.  
  539. #ifdef MSDOS
  540.   /* Set file mode to binary for MSDOS systems */
  541.   setmode(fileno(f), O_BINARY);
  542. #endif /* MSDOS */
  543.  
  544.   /* Allocate and fill structure */
  545.   if ((c = (cfile *)malloc(sizeof(cfile))) != NULL)
  546.   {
  547.     c->f = f;                           /* file stream */
  548.     c->b = 0;                           /* empty fifo (for output) */
  549.     c->c = 0xffffffffL;                 /* preload CRC register */
  550.     c->n = 0;                           /* fifo is empty (output) or */
  551.   }                                     /*  no CRC bytes given (input) */
  552.   return c;
  553. }
  554.  
  555.  
  556.  
  557. /* cgetc(x)--like getc(f), but appends a 32-bit CRC to the end of the stream.
  558.    Return the byte read (the last four of which will be the CRC) or EOF. */
  559. #define cgete(x) (x->n==4?EOF:(x->c=x->n++?x->c>>8:~x->c,(int)x->c&0xff))
  560. #define cgetc(x) (x->n==0&&(b=getc(x->f))!=EOF?(ccnt++,x->c=CRC(x->c,b),b):cgete(c))
  561.  
  562.  
  563. /* cputc(d,x)--like putc(d,f), but delays four bytes and computes a CRC.
  564.    x is a cfile *, and d is expected to be an ulg. */
  565. #define cputf(x) (int)(x->c=CRC(x->c,x->b),putc((int)x->b&0xff,x->f),ccnt++)
  566. #define cputc(d,x) (x->n!=4?x->n++:cputf(x),x->b=(x->b>>8)+((ulg)(d)<<24))
  567.  
  568.  
  569. char *nopath(p)
  570. char *p;                /* full file name */
  571. /* Extract just the name of file--remove and subdirectories or devices */
  572. {
  573. #ifdef MSDOS
  574.   char *q = "/\\:";     /* MSDOS delimiters */
  575. #else /* !MSDOS */
  576. #ifdef VMS
  577.   char *q = "]:";       /* VMS delimiters */
  578. #else /* !VMS */
  579.   char *q = "/";        /* Unix delimiter */
  580. #endif /* ?VMS */
  581. #endif /* ?MSDOS */
  582.   char *r;              /* result of strrchr() */
  583.  
  584.   while (*q)
  585.     if ((r = strrchr(p, *q++)) != NULL)
  586.       p = r + 1;
  587.   return p;
  588. }
  589.  
  590.  
  591. void newship()
  592. /* Open up a new ship file to write to */
  593. {
  594.   int i;                /* scans down name to increment */
  595.  
  596.   for (i = 7; i > 3; i--)
  597.     if (++sname[i] > '9')
  598.       sname[i] = '0';
  599.     else
  600.       break;
  601.   if (i == 3)
  602.     err(SE_MANY, "");
  603.   if ((sfile = fopen(mail ? mktemp(strcpy(mname, TMPNAME)) : sname,
  604.                      "w")) == NULL)
  605.     err(SE_FULL, mail ? mname : sname);
  606.   slns = 0;
  607. }
  608.  
  609.  
  610. void endship(e)
  611. int e;          /* true if ending the last ship file */
  612. /* Finish off current ship file */
  613. {
  614.   char *s;              /* malloc'd space for mail command */
  615.  
  616.   if (ferror(sfile) || fclose(sfile))
  617.     err(SE_FULL, mail ? mname : sname);
  618.   if (mail)
  619.   {
  620.     if ((s = malloc(strlen(MAILCMD)- 5*2 + strlen(mprefix) + strlen(sname) +
  621.                     (e ? 7 : 0) + strlen(mdest) + strlen(mname) + 1)) == NULL)
  622.       err(SE_MEM, "");
  623. #ifdef VMS
  624.     sprintf(s, MAILCMD, mname, mprefix, sname, e ? " (last)" : "", mdest);
  625.     if (!system(s))             /* this string fits on one line */
  626.       err(SE_MAIL, "system() call is not supported on this machine");
  627. #else /* !VMS */
  628.     sprintf(s, MAILCMD, mprefix, sname, e ? " (last)" : "", mdest, mname);
  629.     if (system(s))
  630.       err(SE_MAIL, s);
  631. #endif /* ?VMS */
  632.     free((voidp *)s);
  633.     unlink(mname);
  634.     *mname = 0;
  635.   }
  636. }
  637.  
  638.  
  639. void newline(p)
  640. char *p;                /* name of the input file */
  641. /* Add a new line inside a ship file, possibly cut the file */
  642. {
  643.   putc('\n', sfile);
  644.   slns++;
  645.   if (slmax && slns >= slmax - 2)
  646.   {
  647.     putc('$', sfile);
  648.     if (fast)
  649.       fputs(" f", sfile);
  650.     fputs("\nmore\n", sfile);
  651.     endship(0);
  652.     newship();
  653.     fprintf(sfile, "$%s\ncont %lu %s\n", fast ? " f" : "", ccnt, nopath(p));
  654.     slns += 2;
  655.   }
  656. }
  657.  
  658.  
  659. /* Macro to avoid leading dots.  It assumes i==0 at the beginning of a line
  660.    and that b is an available int.  c is only evaluated once. */
  661. #define sputc(c,f) (i==0?((b=(c))=='.'?putc(' ',f):0,putc(b,f)):putc(c,f))
  662.  
  663.  
  664. void ship(p, f)
  665. char *p;                /* name of the input file */
  666. FILE *f;                /* input file */
  667. /* Encode the binary file f. */
  668. {
  669.   int b;                /* character just read */
  670.   cfile *c;             /* checked file stream */
  671.   int i;                /* how much is written on line so far */
  672.   int j;                /* how much is in bit buffer */
  673.  
  674.   /* Set up output file if needed */
  675.   if ((mail || slmax) && sfile == stdout)
  676.   {
  677.     strcpy(sname, "part0000");
  678.     newship();
  679.   }
  680.  
  681.   /* Write header */
  682.   if ((c = chook(f)) == NULL)
  683.     err(SE_MEM, "");
  684.   ccnt = 0;
  685.   if (slmax && slns >= slmax - 5) 
  686.   {
  687.     endship(0);
  688.     newship();
  689.   }
  690.   fprintf(sfile, "$%s\nship %s\n", fast ? " f" : "", nopath(p));
  691.   slns += 2;
  692.  
  693.   /* Encode the file, writing to sfile */
  694.   if (fast)
  695.   {
  696.     int d;              /* accumulates bits (never more than 14) */
  697.  
  698.     d = j = i = 0;
  699.     while ((b = cgetc(c)) != EOF)
  700.     {
  701.       d |= b << j;
  702.       j += 8;
  703.       if ((d & 0x3f) >= LOWSZ)
  704.       {
  705.         sputc((int)(safe[(d & 0x3f) + LOWSZ]), sfile);
  706.         d >>= 6;
  707.         j -= 6;
  708.       }
  709.       else
  710.       {
  711.         sputc((int)(safe[(d & 0x3f) + (d & 0x40 ? LOWSZ : 0)]), sfile);
  712.         d >>= 7;
  713.         j -= 7;
  714.       }
  715.       if (++i == 79)
  716.       {
  717.         newline(p);
  718.         i = 0;
  719.       }
  720.       if (j >= 6 && (d & 0x3f) >= LOWSZ)
  721.       {
  722.         sputc((int)(safe[(d & 0x3f) + LOWSZ]), sfile);
  723.         d >>= 6;
  724.         j -= 6;
  725.         if (++i == 79)
  726.         {
  727.           newline(p);
  728.           i = 0;
  729.         }
  730.       }
  731.       else if (j >= 7)
  732.       {
  733.         sputc((int)(safe[(d & 0x3f) + (d & 0x40 ? LOWSZ : 0)]), sfile);
  734.         d >>= 7;
  735.         j -= 7;
  736.         if (++i == 79)
  737.         {
  738.           newline(p);
  739.           i = 0;
  740.         }
  741.       }
  742.     }
  743.     free((voidp *)c);
  744.  
  745.     /* Write leftover bits */
  746.     if (j)
  747.     {
  748.       sputc((int)(safe[d + (d < LOWSZ ? 0 : LOWSZ)]), sfile);
  749.       putc('\n', sfile);
  750.       slns++;
  751.     }
  752.     else if (i)
  753.     {
  754.       putc('\n', sfile);
  755.       slns++;
  756.     }
  757.   }
  758.   else
  759.   {
  760.     ulg d;              /* accumulates bytes */
  761.  
  762.     d = j = i = 0;
  763.     while ((b = cgetc(c)) != EOF)
  764.     {
  765.       d += ((ulg)b) << j;
  766.       if ((j += 8) == 32)
  767.       {
  768.         sputc((int)(safe[(int)(d % 85)]), sfile);  d /= 85;
  769.         putc((int)(safe[(int)(d % 85)]), sfile);  d /= 85;
  770.         putc((int)(safe[(int)(d % 85)]), sfile);  d /= 85;
  771.         putc((int)(safe[(int)(d % 85)]), sfile);  d /= 85;
  772.         putc((int)(safe[(int)d]), sfile);
  773.         if (++i == 15)                  /* each line is <= 75 characters */
  774.         {
  775.           newline(p);
  776.           i = 0;
  777.         }
  778.         d = j = 0;
  779.       }
  780.     }
  781.     free((voidp *)c);
  782.   
  783.     /* Write leftover data */
  784.     if (j)
  785.     {
  786.       j >>= 3;
  787.       sputc((int)(safe[(int)(d % 85)]), sfile);
  788.       while (j--)
  789.       {
  790.         d /= 85;
  791.         putc((int)(safe[(int)(d % 85)]), sfile);
  792.       }
  793.       putc('\n', sfile);
  794.       slns++;
  795.     }
  796.     else if (i)
  797.     {
  798.       putc('\n', sfile);
  799.       slns++;
  800.     }
  801.   }
  802.   putc('$', sfile);
  803.   if (fast)
  804.     fputs(" f", sfile);
  805.   fputs("\nend\n", sfile);
  806.   slns += 2;
  807.   if (ferror(sfile) || fflush(sfile))
  808.     err(SE_FULL, mail ? mname : sname);
  809.   if (noisy)
  810.     fprintf(stderr, "%s shipped\n", p);
  811. }
  812.  
  813.  
  814. void mkinv()
  815. /* Build invsafe[], the inverse of safe[]. */
  816. {
  817.   int i;
  818.  
  819.   for (i = 0; i < 256; i++)
  820.     invsafe[i] = 127;
  821.   for (i = 0; i < sizeof(safe); i++)
  822.     invsafe[safe[i]] = (char)i;
  823.   for (i = 0; aliases[i]; i += 2)
  824.     invsafe[aliases[i]] = invsafe[aliases[i + 1]];
  825. }
  826.  
  827.  
  828. unsigned int decb;      /* bit buffer for decode */
  829. unsigned int decn;      /* number of bits in decb */
  830.  
  831. void decode(s, c)
  832. unsigned char *s;       /* data to decode */
  833. cfile *c;               /* binary output file */
  834. /* Decode s, a string of base 85 digits or, if fast is true, a string of safe
  835.    characters generated arithmetically, into its binary equivalent, writing
  836.    the result to c, using cputc(). */
  837. {
  838.   int b;                /* state of line loop, next character */
  839.   int k;                /* counts bits or digits read */
  840.   /* powers of 85 table for decoding */
  841.   static ulg m[] = {1L,85L,85L*85L,85L*85L*85L,85L*85L*85L*85L};
  842.  
  843.   if (fast)
  844.   {
  845.     unsigned int d;     /* disperses bits */
  846.  
  847.     d = decb;
  848.     k = decn;
  849.     while ((b = *s++) != 0)
  850.       if ((b = invsafe[b]) < sizeof(safe))
  851.       {
  852.         if (b < LOWSZ)
  853.         {
  854.           d |= b << k;
  855.           k += 7;
  856.         }
  857.         else if ((b -= LOWSZ) < LOWSZ)
  858.         {
  859.           d |= (b + 0x40) << k;
  860.           k += 7;
  861.         }
  862.         else
  863.         {
  864.           d |= b << k;
  865.           k += 6;
  866.         }
  867.         if (k >= 8)
  868.         {
  869.           cputc(d, c);
  870.           d >>= 8;
  871.           k -= 8;
  872.         }
  873.       }
  874.     decb = d;
  875.     decn = k;
  876.   }
  877.   else
  878.   {
  879.     ulg d;              /* disperses bytes */
  880.  
  881.     d = k = 0;
  882.     while ((b = *s++) != 0)
  883.       if ((b = invsafe[b]) < 85)
  884.       {
  885.         d += m[k] * b;
  886.         if (++k == 5)
  887.         {
  888.           cputc(d, c);  d >>= 8;
  889.           cputc(d, c);  d >>= 8;
  890.           cputc(d, c);  d >>= 8;
  891.           cputc(d, c);
  892.           d = k = 0;
  893.         }
  894.       }
  895.     if (--k > 0)
  896.     {
  897.       while (--k)
  898.       {
  899.         cputc(d, c);
  900.         d >>= 8;
  901.       }
  902.       cputc(d, c);
  903.     }
  904.   }
  905. }
  906.  
  907.  
  908. void unship(v, g, o)
  909. char **v;               /* arguments */
  910. int g;                  /* number of arguments */
  911. int o;                  /* overwrite flag */
  912. /* Extract from the files named in the arguments the files that were
  913.    encoded by ship.  If an argument is "-", then stdin is read. */
  914. {
  915.   int b;                /* state of line loop */
  916.   cfile *c;             /* output binary file */
  917.   FILE *f;              /* output file */
  918.   char *h;              /* name of current ship file */
  919.   char l[LNSZ];         /* line buffer on input */
  920.   int n;                /* next argument to use for input */
  921.   char *p;              /* modifies line buffer */
  922.   char *q;              /* scans continuation line */
  923.   char *r;              /* name of output binary file */
  924.   FILE *s;              /* current ship file */
  925.   int z;                /* true if zero files received */
  926.  
  927.   /* Build inverse table */
  928.   mkinv();
  929.  
  930.   /* No input or output files initially */
  931.   s = NULL;
  932.   c = NULL;
  933.   h = r = NULL;
  934.  
  935.   /* Loop on input files' lines */
  936.   z = 1;                                /* none received yet */
  937.   n = 0;                                /* start with file zero */
  938.   b = 2;                                /* not in body yet */
  939.   while (1)                             /* return on end of last file */
  940.   {
  941.     /* Get next line from list of files */
  942.     while (s == NULL || fgets(l, LNSZ, s) == NULL)
  943.     {
  944.       if (s != NULL)
  945.         fclose(s);
  946.       if (n >= g)
  947.       {
  948.         if (c != NULL)
  949.           err(SE_PART, r);
  950.         else if (z)
  951.           err(SE_NONE, "");
  952.         return;
  953.       }
  954.       if (v[n][0] == '-')
  955.         if (v[n][1])
  956.           err(SE_ARG, v[n]);
  957.         else
  958.         {
  959.           h = "stream stdin";
  960.           s = stdin;
  961.         }
  962.       else
  963.       {
  964.         h = v[n];
  965.         if ((s = fopen(h, "r")) == NULL)
  966.           err(SE_FIND, h);
  967.       }
  968.       n++;
  969.       b &= ~1;                          /* not in middle of line */
  970.     }
  971.  
  972.     /* Strip control characters and leading blank space, if any */
  973.     for (q = l; *q && *q <= ' ' && *q != '\n'; q++)
  974.       ;
  975.     for (p = l; *q; q++)
  976.       if (*q >= ' ' || *q == '\n')
  977.         *p++ = *q;
  978.     *p = 0;
  979.  
  980.     /* Based on current state, end or start on terminator.  States are:
  981.          b == 0:  at start of body or body terminator line
  982.          b == 1:  in middle of body line
  983.          b == 2:  at start of non-body line
  984.          b == 3:  in middle of non-body line
  985.          b == 4:  at information line
  986.     */
  987.     switch (b)
  988.     {
  989.     case 0:
  990.       if ((!fast && strcmp(l, "$\n") == 0) ||
  991.           (fast && strcmp(l, "$ f\n") == 0))
  992.       {
  993.         b = 4;
  994.         break;
  995.       }
  996.       /* fall through to case 1 */
  997.     case 1:
  998.       decode((unsigned char *)l, c);
  999.       b = l[strlen(l) - 1] != '\n';
  1000.       break;
  1001.     case 2:
  1002.       if (strcmp(l, "$\n") == 0 || strcmp(l, "$ f\n") == 0)
  1003.       {
  1004.         fast = l[1] == ' ';
  1005.         b = 4;
  1006.         break;
  1007.       }
  1008.       /* fall through to case 3 */
  1009.     case 3:
  1010.       b = l[strlen(l)-1] == '\n' ? 2 : 3;
  1011.       break;
  1012.     case 4:
  1013.       /* Possible information lines are ship, more, cont, and end */
  1014.       if (l[b = strlen(l) - 1] != '\n')
  1015.         err(SE_FORM, h);
  1016.       l[b] = 0;
  1017.       if (strncmp(l, "ship ", 5) == 0)
  1018.       {
  1019.         /* get name, open new output file */
  1020.         if (c != NULL)
  1021.           err(SE_FORM, h);
  1022.         if ((r = malloc(b - 4)) == NULL)
  1023.           err(SE_MEM, "");
  1024.         strcpy(r, l + 5);
  1025.         if (strcmp(r, "-") == 0)
  1026.           f = stdout;
  1027. #ifndef VMS     /* shouldn't have explicit version #, so VMS won't overwrite */
  1028.         else if (!o && (f = fopen(r, "r")) != NULL)
  1029.         {
  1030.           fclose(f);
  1031.           err(SE_OVER, r);
  1032.         }
  1033. #endif /* !VMS */
  1034.         else if ((f = fopen(r, "w")) == NULL)
  1035.           err(SE_FULL, r);
  1036.         if ((c = chook(f)) == NULL)
  1037.           err(SE_MEM, "");
  1038.         b = decb = decn = 0;
  1039.         ccnt = 0;
  1040.       }
  1041.       else if (strcmp(l, "more") == 0)
  1042.       {
  1043.         /* check if currently writing */
  1044.         if (c == NULL)
  1045.           err(SE_FORM, h);
  1046.         b = 2;
  1047.       }
  1048.       else if (strncmp(l, "cont ", 5) == 0)
  1049.       {
  1050.         /* check name and file offset */
  1051.         if (c == NULL)
  1052.           err(SE_FORM, h);
  1053.         for (q = l + 5; *q && *q != ' '; q++)
  1054.           ;
  1055.         if (*q == 0 || atol(l + 5) != ccnt + 4 + (decn != 0) ||
  1056.             strcmp(q + 1, r))
  1057.           err(SE_CONT, r);
  1058.         b = 0;
  1059.       }
  1060.       else if (strcmp(l, "end") == 0)
  1061.       {
  1062.         /* check crc, close output file */
  1063.         if (c == NULL)
  1064.           err(SE_FORM, h);
  1065.         if (c->n != 4 || c->b != ~c->c)
  1066.           err(SE_CRC, r);
  1067.         if (ferror(c->f) || fclose(c->f))
  1068.           err(SE_FULL, r);
  1069.         if (noisy)
  1070.           fprintf(stderr, "%s received\n", r);
  1071.         z = 0;
  1072.         free((voidp *)c);
  1073.         c = NULL;
  1074.         b = 2;
  1075.       }
  1076.       else
  1077.       {
  1078.         for (q = l; *q && *q != ' '; q++)
  1079.           ;
  1080.         *q = 0;
  1081.         fprintf(stderr, "%s: unsupported keyword '%s' ignored\n", warname, l);
  1082.         b = 4;
  1083.       }
  1084.       break;
  1085.     }
  1086.   }
  1087. }
  1088.  
  1089.  
  1090. void help()
  1091. {
  1092.   int i;
  1093.   static char *text[] = {
  1094. "Usage:",
  1095. "   ship [-f] [-q] [-nnn] [-m address] [-s subject] files...",
  1096. "",
  1097. "   ships the files to stdout.  -m sends the output via the mailer to",
  1098. "   address.  -nnn splits the output into pieces of nnnK bytes or less.",
  1099. "   if -nnn is used without -m, the output goes to the files partxxxx,",
  1100. "   where xxxx is 0001, 0002, etc.  If -0 is specified, the output goes",
  1101. "   entirely to the file part0001.  -f uses a fast method with slightly",
  1102. "   less performance.  If no files are given, stdin is used.  The special",
  1103. "   filename '-' also takes input from stdin.  Files shipped from stdin",
  1104. "   are unshipped to stdout.  This can be used to document a shipment.",
  1105. "   When mailing, -s gives a subject line prefix.  -q inhibits messages.",
  1106. "",
  1107. "   ship -u [-o] [-q] files...",
  1108. "   unship  [-o] [-q] files...",
  1109. "",
  1110. "   extracts the contents of the mail messages in files...  -o allows",
  1111. "   existing files to be overwritten.  -u is implied if the name of the",
  1112. "   executable is unship.  If no files are given, the input is from",
  1113. "   stdin.  If any of the files were shipped from stdin, then they are",
  1114. "   extracted to stdout."
  1115.   };
  1116.  
  1117.   puts(SHIPVER);
  1118.   for (i = 0; i < sizeof(text)/sizeof(char *); i++)
  1119.   {
  1120.     printf(text[i]);
  1121.     putchar('\n');
  1122.   }
  1123.   exit(0);
  1124. }
  1125.  
  1126.  
  1127. void main(argc, argv)
  1128. int argc;               /* number of arguments */
  1129. char **argv;            /* table of argument strings */
  1130. {
  1131.   FILE *f;              /* input file */
  1132.   char *p;              /* temporary variable */
  1133.   int o;                /* overwrite flag */
  1134.   int r;                /* temporary variable */
  1135.   int s;                /* true if no names given */
  1136.  
  1137.   /* No temporary file yet (for err()) */
  1138.   *mname = 0;
  1139.  
  1140.   /* No subject prefix yet */
  1141.   *mprefix = 0;
  1142.  
  1143.   /* See if help requested */
  1144.   if (argc > 1 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-?") == 0))
  1145.     help();
  1146.  
  1147.   /* Unship */
  1148.   if ((p = strrchr(argv[0], PATHCUT)) == NULL)
  1149.     p = argv[0];
  1150.   else
  1151.     p++;
  1152.   r = 0;                                /* (make some compilers happier) */
  1153.   if ((r = strncmp(p, "unship", 6)) == 0 ||
  1154.       (r = strncmp(p, "UNSHIP", 6)) == 0 ||
  1155.       (argc > 1 && strcmp(argv[1], "-u") == 0))
  1156.   {
  1157.     errname = "unship error";
  1158.     warname = "unship warning";
  1159.     r = r ? 2 : 1;                      /* next arg */
  1160.     o = 0;                              /* disallow overwriting */
  1161.     if (r < argc && strcmp(argv[r], "-o") == 0)
  1162.     {
  1163.       r++;
  1164.       o = 1;                            /* allow overwriting */
  1165.     }
  1166.     if (r < argc && strcmp(argv[r], "-q") == 0)
  1167.     {
  1168.       r++;
  1169.       noisy = 0;                        /* inhibit messages */
  1170.     }
  1171.     if (r < argc)
  1172.       unship(argv + r, argc - r, o);    /* unship files in args */
  1173.     else
  1174.     {
  1175.       char *a[1];       /* short list of names (one) */
  1176.  
  1177.       a[0] = "-";
  1178.       unship(a, 1, o);                  /* no args--do stdin */
  1179.     }
  1180.   }
  1181.  
  1182.   /* Ship */
  1183.   else
  1184.   {
  1185.     mail = 0;                           /* not mailing */
  1186.     fast = 0;                           /* use base 85 encoding */
  1187.     s = 1;                              /* no names given yet */
  1188.     strcpy(sname, "-");                 /* output to stdout */
  1189.     sfile = stdout;
  1190.     slns = slmax = 0;
  1191.     for (r = 1; r < argc; r++)          /* go through args */
  1192.       if (argv[r][0] == '-')            /* option or stdin */
  1193.         if (argv[r][1])                 /* option */
  1194.         {
  1195.           if (argv[r][1] == 'm')        /* mail output */
  1196.           {
  1197.             mail = 1;
  1198.             mdest = NULL;               /* next arg is mail address */
  1199.           }
  1200.           else if (argv[r][1] == 's')   /* next arg is subject prefix */
  1201.             mprefix = NULL;
  1202.           else if (argv[r][1] == 'f')   /* fast arithmetic encoding */
  1203.             fast = 1;
  1204.           else if (argv[r][1] == 'q')   /* quiet operation */
  1205.             noisy = 0;
  1206.           else                          /* option is number of lines */
  1207.           {
  1208.             /* Check numeric option */
  1209.             for (p = argv[r] + 1; *p; p++)
  1210.               if (*p < '0' || *p > '9')
  1211.                 break;
  1212.             if (*p || slmax)
  1213.               err(SE_ARG, argv[r]);
  1214.   
  1215.             /* Zero means infinity, else convert */
  1216.             if ((slmax = atol(argv[r] + 1)) == 0)
  1217.               slmax = -1L;
  1218.             else
  1219.             {
  1220.               long b;
  1221.  
  1222.               b = slmax * 1000L;
  1223.               slmax = (int)(b / (fast ? 81 : 77));
  1224.               /* Note: five of the lines aren't that long, but that
  1225.                  leaves some slack for mail headers, etc.  Also, note
  1226.                  that we conservatively assume 1000 bytes/K and two
  1227.                  bytes per new line. */
  1228.             }
  1229.           }
  1230.         }
  1231.         else                            /* input file is stdin */
  1232.         {
  1233.           if (mail && mdest == NULL)
  1234.             err(SE_ARG, "- (no mail destination given)");
  1235.           s = 0;
  1236.           if (mail && !*mprefix)
  1237.             strcpy(mprefix, "(stdin)");
  1238.           ship("-", stdin);
  1239.         }
  1240.       else                              /* not option or stdin */
  1241.         if (mail && mdest == NULL)      /* arg is mail address */
  1242.           mdest = argv[r];
  1243.         else if (mprefix == NULL)       /* arg is subject prefix */
  1244.           mprefix = argv[r];
  1245.         else                            /* arg is file to ship */
  1246.         {
  1247.           s = 0;
  1248.           if ((f = fopen(argv[r], "r")) == NULL)
  1249.             err(SE_FIND, argv[r]);
  1250.           if (mail && !*mprefix)
  1251.           {
  1252.             int i;
  1253.  
  1254.             for (i = 0, p = nopath(argv[r]); i < 8 && *p; p++)
  1255.               if ((*p >= '0' && *p <= '9') || (*p >= 'A' && *p <= 'Z') ||
  1256.                   (*p >= 'a' && *p <= 'z') || *p == '.' || *p == '_')
  1257.                 mprefix[i++] = *p;
  1258.             mprefix[i] = 0;
  1259.           }
  1260.           ship(argv[r], f);
  1261.           fclose(f);
  1262.         }
  1263.     if (s)                              /* no names--act as filter */
  1264.       if (mail && mdest == NULL)
  1265.         err(SE_ARG, "-m (no mail destination given)");
  1266.       else if (mprefix == NULL)
  1267.         err(SE_ARG, "-s (no subject prefix given)");
  1268.       else
  1269.       {
  1270.         if (mail && !*mprefix)
  1271.           strcpy(mprefix, "(stdin)");
  1272.         ship("-", stdin);
  1273.       }
  1274.     endship(1);                         /* clean up */
  1275.     if (noisy && (mail || slmax))
  1276.       fprintf(stderr, "file%s%s %s\n",
  1277.               strcmp("part0001", sname) ? "s part0001.." : " ", sname,
  1278.               mail ? "mailed" : "written");
  1279.   }
  1280.  
  1281.   /* Done */
  1282.   exit(0);
  1283. }
  1284.